import {
  getSaleIndexCard, // 获取销售指标卡数据
  getSaleTrend, // 销售趋势
  getSaleOrderPrice, // 订单单价分析
  getSaleReturn, // 销售退货分析
  getStorageTime, // 库龄
  getStorageTimePie, // 库龄饼图
  getIndexGoodsLossAndSaleRate, // 获取指标卡商品报损数量和库销比(指标卡)
  getIndexGoodNumberAndMoney, // 获取指标卡商品数量和库存金额
  getBuyKpi, // 进货指标(顶端数据)buyKpi
  getBuyTrend, // 进货趋势
  getBuyOrderPriceRate, // 进货订单单价比率
  // getInventoryChart, // 测试
} from '@/api/operation'

const getOperationSata = () => {
  return {
    saleTrendData: {}, // 销售趋势
    saleOrderPriceData: {}, // 订单单价分析
    saleReturnData: {}, // 销售退货分析
    saleReturnLengeds: {}, // 退货图例
    storageTimeData: {}, // 库龄数据
    storageTimePieData: {}, // 库龄饼图数据
    buyList: '', // 进货顶端数据
    lossAndSaleList: '', // 获取指标卡商品报损数量和库销比(指标卡)
    buyTrendData: {}, // 进货趋势
    buyOrderPriceRateData: {}, // 进货订单单价比率
    type: '',
    size: 10,
    current: 1,
    inventoryTable: [],
    storageSection: { // 库龄区间
      min: 0,
      max: 0,
    }
  }
}

const state = getOperationSata()

const mutations = {
  RESET_STATE: state => {
    Object.assign(state, getOperationSata())
  },
  // 更新state中的type
  UPDATE_CONDITIONS: (state, payload = {}) => {
    const { type } = state
    state.type = payload ? { ...type, ...payload } : {}
  },
}

const actions = {
  // 获取销售指标卡数据
  async getSaleIndexCard({state}, payload = {}) {
    state.loading = true
    const {retData} = await getSaleIndexCard(payload)
    Object.assign(state, {
      loading: false,
      buyList: retData || {}
    })
  },
  // 销售趋势
  async getSaleTrend({state}, payload = {}) {
    state.loading = true
    const {retData} = await getSaleTrend(payload)
    const rows = retData || []
    const columns = ['xxAlias', 'amount']
    Object.assign(state, {
      loading: false,
      saleTrendData: {
        rows,
        columns
      },
    })
  },
  // 订单单价分析
  async getSaleOrderPrice({state}, payload) {
    const {retData} = await getSaleOrderPrice({payload})
    const rows = retData || []
    const columns = ['name', 'value']
    Object.assign(state, {
      saleOrderPriceData: {
        rows,
        columns
      }
    })
  },
  // 销售退货分析
  async getSaleReturn({state}, payload) {
    const {retData} = await getSaleReturn({payload})
    // const retData = [{'xxAlias': '路由器-11', 'returnNumbers': 4, 'saleReturnRatio': 0.5}, 
    //   {'xxAlias': 'DS-2CD763PF-E(2.7-9mm)(28181标配)测试', 'returnNumbers': 10, 'saleReturnRatio': 0.6},
    //   {'xxAlias': 'DS-7904HF-SH(标配)【即将停产，不推荐】', 'returnNumbers': 2, 'saleReturnRatio': 1},
    //   {'xxAlias': 'DS-7816N-SH(标配)', 'returnNumbers': 2, 'saleReturnRatio': 0.3},
    //   {'xxAlias': '测试商品0002', 'returnNumbers': 7, 'saleReturnRatio': 0.7}
    // ]
    const rows = retData || []
    const columns = ['xxAlias', 'returnNumbers', 'saleReturnRatio']
    Object.assign(state, {
      saleReturnData: {
        rows,
        columns
      },
    })
  },
  // 库龄分析
  async getStorageTime({state}) {
    const obj = {filed: 'qty'}
    const payload = {...state.storageSection, ...obj}
    const {retData = []} = await getStorageTime(payload)
    const rows = retData
    const columns = ['xxAlias', 'value']
    Object.assign(state, {
      storageTimeData: {
        rows,
        columns,
      }
    })
  },
  // 库龄分析饼图
  async getStorageTimePie({state}) {
    const payload = state.storageSection
    const {retData} = await getStorageTimePie(payload)
    const rows = retData || []
    const columns = ['name', 'value']
    for (let i = 0; i < rows.length; i++) {
      const newName = rows[i].name + rows[i].unit
      rows[i]['name'] = newName
    }
    Object.assign(state, {
      storageTimePieData: {
        rows,
        columns
      }
    })
  },
  // 获取指标卡商品数量和库存金额
  async getIndexGoodNumberAndMoney({state}, payload = {}) {
    state.loading = true
    const {retData} = await getIndexGoodNumberAndMoney(payload)
    Object.assign(state, {
      loading: false,
      buyList: retData || {}
    })
  },
  // 获取指标卡商品报损数量和库销比(指标卡)
  async getIndexGoodsLossAndSaleRate({state}, payload = {}) {
    state.loading = true
    const {retData} = await getIndexGoodsLossAndSaleRate(payload)
    Object.assign(state, {
      loading: false,
      lossAndSaleList: retData || {}
    })
  },
  // 进货指标(顶端数据)buyKpi
  async getBuyKpi({state}, payload = {}) {
    state.loading = true
    const {retData} = await getBuyKpi(payload)
    Object.assign(state, {
      loading: false,
      buyList: retData || {}
    })
  },
  // 进货趋势
  async getBuyTrend({state}, payload = {}) {
    state.loading = true
    const {retData} = await getBuyTrend(payload)
    const rows = retData || []
    const columns = ['xxAlias', 'buyAmount']
    Object.assign(state, {
      loading: false,
      buyTrendData: {
        rows,
        columns
      }
    })
  },
  // 进货订单单价比率
  async getBuyOrderPriceRate({state}, payload) {
    const {retData} = await getBuyOrderPriceRate({payload})
    const rows = retData || []
    const columns = ['name', 'value']
    Object.assign(state, {
      buyOrderPriceRateData: {
        rows,
        columns
      }
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
  
